<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备管理后台</title>
    <style>
        /* 基础样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Roboto, sans-serif;
        }
 
        /* 布局容器 */
        .container {
            display: flex;
            min-height: 100vh;
            background: #f5f6fa;
        }
 
        /* 侧边栏 */
        .sidebar {
            width: 240px;
            background: #ffffff;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            padding: 20px 0;
        }
 
        /* 主内容区 */
        .main-content {
            flex: 1;
            padding: 30px;
        }
 
        /* 顶部导航 */
        .header {
            background: #fff;
            padding: 15px 30px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
 
        /* 导航菜单 */
        .nav-menu {
            list-style: none;
        }
        .nav-item {
            padding: 12px 24px;
            color: #2d3436;
            transition: 0.3s;
            cursor: pointer;
        }
        .nav-item:hover {
            background: #f1f2f6;
            border-left: 4px solid #3498db;
        }
 
        /* 统计卡片 */
        .stats-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        /* 操作按钮 */
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: 0.3s;
        }
 
        .btn-primary {
            background: #3498db;
            color: white;
        }
 
        .btn-danger {
            background: #e74c3c;
            color: white;
        }

        input, select, textarea{
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .time-input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 10px;
            color: #2d3436;
            transition: border-color 0.3s ease;
        }
 
        .time-input:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52,152,219,0.2);
        }
 
        /*label {
            display: block;
            margin-bottom: 8px;
            color: #636e72;
            font-size: 13px;
            font-weight: 500;
        }*/
 
        /* 自定义时间选择器弹出样式 */
        input[type="datetime-local"]::-webkit-calendar-picker-indicator {
            filter: invert(0.5);
            padding: 4px;
            cursor: pointer;
        }
 
        input[type="datetime-local"]::-webkit-datetime-edit {
            padding: 2px;
        }


    </style>
    <script type="text/javascript">
        var parser = new DOMParser();

        var touch_row = `
            <tr style="border-bottom: 1px solid #eee;">
                <td style="padding: 12px;">
                    <select id = "touch_number">
                        <option value="1">J1</option>
                        <option value="2">J2</option>
                        <option value="3">J3</option>
                        <option value="4">J4</option>
                        <option value="5">J5</option>
                        <option value="6">J6</option>
                        <option value="7">J7</option>
                        <option value="8">J8</option>
                        <option value="9">J9</option>
                        <option value="10">J10</option>
                        <option value="11">J11</option>
                        <option value="12">J12</option>
                        <option value="13">J13</option>
                        <option value="14">J14</option>
                        <option value="15">J15</option>
                        <option value="16">J16</option>
                    </select>
                </td>
                <td style="padding: 12px;">
                    <select id="touch_action">
                        <option value="touch_once">单击</option>
                        <option value="touch_double">双击</option>
                        <option value="touch_down">按下</option>
                        <option value="touch_up">抬起</option>
                    </select>
                </td>
                <td style="padding: 12px;color: #2ecc71;">
                    <input type="text" id="touch_sleep" value="10"/>
                </td>
                <td style="padding: 12px;color: #2ecc71;">
                    <input type="text" id="touch_count" value="1"/>
                </td>
                <td style="padding: 12px;color: #2ecc71;">
                    <button class="btn btn-primary" onclick="addTouchRow(this.parentNode.parentNode)">新增</button>
                    <button class="btn btn-danger" onclick="deleteRow(this.parentNode.parentNode)">删除</button>
                </td>
            </tr>
        `
        document.addEventListener('DOMContentLoaded',  function() {
            touch_tbody.insertAdjacentHTML('beforeend', touch_row)
            loadconfiglist(true, undefined)
        });

        remote_config_map = {}
        function loadconfiglist(showfirst, showconfigname) {
            post('/touch_config_list', '{}', function(state, responseText){
                remote_config_list = JSON.parse(responseText);
                configlist.innerHTML = ''
                loadconfigname = undefined
                for(var config_index in remote_config_list) {
                    config_name = remote_config_list[config_index]['config_name']
                    remote_config_map[config_name] = remote_config_list[config_index]
                    configlist.insertAdjacentHTML('beforeend', '<option value="'+config_name+'">'+config_name+'</option>')
                    if(showfirst == true || showconfigname == config_name) {
                        loadconfigname = config_name
                    }
                }
                if(loadconfigname) {
                    loadconfig(config_name)
                }
            })
        }

        function loadconfig(config_name) {

            var config = remote_config_map[config_name]
            
            // 方案名称
            configname.value = config_name
            touch_config_time.value = config['touch_config_time']
            touch_config_count.value = config['touch_config_count']
            touch_config_result.value = config['touch_config_result']

            
            // 方案动作
            var configrows = config['config_rows']
            touch_tbody.innerHTML = ''
            for(i in configrows) {
                var configrow = configrows[i]
                touch_tbody.insertAdjacentHTML('beforeend', touch_row)
                var lastchild = touch_tbody.querySelector("tr:last-of-type")
                lastchild.querySelector("#touch_number").value = configrow['touch_number']
                lastchild.querySelector("#touch_action").value = configrow['touch_action']
                lastchild.querySelector("#touch_sleep").value  = configrow['touch_sleep']
                lastchild.querySelector("#touch_count").value  = configrow['touch_count']
            }

            configlist.value = config_name
        }

        function addTouchRow(ele){
            ele.insertAdjacentHTML('afterend', touch_row)
        }

        function deleteRow(ele) {
            ele.parentNode.removeChild(ele); // 移除该行
        }

        function formsubmit(submit_type) {
            var form = {}

            // 是否立即执行，0删除，1保存并执行，2保存，3执行
            action_name = ""
            if(submit_type == 0) {
                action_name = "/touch_config_del"
            } else if (submit_type == 1) {
                action_name = "/touch_config_save_action"
            } else if (submit_type == 2) {
                action_name = "/touch_config_save"
            } else if (submit_type == 3) {
                action_name = "/touch_config_action"
            }

            form['submit_type'] = submit_type

            // 配置名称
            form['config_name'] = configname.value
            form['touch_config_time'] = touch_config_time.value
            form['touch_config_count'] = touch_config_count.value
            form['touch_config_result'] = touch_config_result.value

            // 执行指令
            var configrows = []
            for(var i = 0; i < touch_tbody.childNodes.length; i++) {
                var child = touch_tbody.childNodes[i];
                if(!child || child.tagName != 'TR') {
                    continue;
                }
                var configrow = {}
                configrow['touch_number'] = child.querySelector("#touch_number").value
                configrow['touch_action'] = child.querySelector("#touch_action").value
                configrow['touch_sleep']  = child.querySelector("#touch_sleep").value
                configrow['touch_count']  = child.querySelector("#touch_count").value
                // console.log(JSON.stringify(configrow))
                configrows.push(configrow)
            }
            form['config_rows'] = configrows
            // console.log(JSON.stringify(configrows))

            post(action_name + '?config_name=' + configname.value, form, function(state, responseText){
                if(state == 1) {
                    // 请求成功，处理响应数据
                    formsubmitret.value = responseText
                } else {
                    // 请求失败，处理错误情况
                    formsubmitret.value = "Error:" + responseText
                    console.error("Error:" + responseText);
                }

                if(submit_type == 0) {
                    loadconfiglist(true, undefined)
                } else {
                    loadconfiglist(false, configname.value)
                }
            })
        }

        function post(url, jsonData, callback) {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xhr.send(JSON.stringify(jsonData));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    callback(1, xhr.responseText)
                } else if (xhr.readyState === 4) {
                    callback(0, xhr.status)
                }
            };
        }
    </script>
    <script type="text/javascript">
        function menu(index) {
            touch_config.style.display = 'none'
            webhook_config.style.display = 'none'
            wifi_config.style.display = 'none'
            // 方案配置
            if(index == 1) {
                touch_config.style.display = 'block'
            // 通知配置
            } else if(index == 2) {
                webhook_config.style.display = 'block'
            // WIFI配置
            } else if(index == 3) {
                wifi_config.style.display = 'block'
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <!-- 侧边栏 -->
        <aside class="sidebar">
            <h2 style="padding: 0 24px;margin-bottom: 30px;color: #3498db;">设备管理</h2>
            <ul class="nav-menu">
                <li class="nav-item" onclick="menu(1)">方案配置</li>
                <!-- <li class="nav-item" onclick="menu(2)">通知配置</li>
                <li class="nav-item" onclick="menu(3)">WIFI配置</li> -->
            </ul>
        </aside>
 
        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部导航 -->
            <header class="header">
                <h3>方案配置</h3>
                <div class="user-info">
                    <span><a href="http://fengmm521.taobao.com/">产品购买:https://fengmm521.taobao.com/</a></span>
                    <i class="notification-icon">🔔</i>
                </div>
            </header>
 
            <!-- 数据表格 -->
            <div class="stats-card" style="margin-top: 30px;">
                <table id="touch_config" style="width: 100%;margin-top: 15px;border-collapse: collapse;">
                    <tbody>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">方案列表</td>
                            <td style="padding: 12px;"><select id="configlist" onchange="loadconfig(this.value)" /></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">方案名称</td>
                            <td style="padding: 12px;"><input type="text" id="configname"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">动作配置</td>
                            <td style="padding: 12px;">
                                <table style="width: 100%;margin-top: 15px;border-collapse: collapse;">
                                    <thead>
                                        <tr style="border-bottom: 1px solid #eee;">
                                            <th style="padding: 12px;text-align: left;">触发位</th>
                                            <th style="padding: 12px;text-align: left;">动作</th>
                                            <th style="padding: 12px;text-align: left;">等待时间</th>
                                            <th style="padding: 12px;text-align: left;">动作次数</th>
                                            <th style="padding: 12px;text-align: left;">增加/删除</th>
                                        </tr>
                                    </thead>
                                    <tbody id="touch_tbody">
                                        
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">定时执行</td>
                            <td style="padding: 12px;"><input type="datetime-local" 
                               id="touch_config_time" 
                               class="time-input"
                               step="1"
                               required/>
                            </td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">方案次数</td>
                            <td style="padding: 12px;"><input type="text" id="touch_config_count" value="1"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">已经执行</td>
                            <td style="padding: 12px;"><input type="text" id="touch_config_result" value="0"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;">执行结果</td>
                            <td style="padding: 12px;"><textarea id="formsubmitret"></textarea></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;" colspan="2">
                                <button class="btn btn-primary" onclick="formsubmit(3)">执行</button>&nbsp;&nbsp;&nbsp;&nbsp;
                                <button class="btn btn-primary" onclick="formsubmit(2)">保存</button>&nbsp;&nbsp;&nbsp;&nbsp;
                                <button class="btn btn-primary" onclick="formsubmit(1)">保存并执行</button>&nbsp;&nbsp;&nbsp;&nbsp;
                                <button class="btn btn-danger" onclick="formsubmit(0)">删除配置</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table id="webhook_config" style="width: 100%;margin-top: 15px;border-collapse: collapse; display: none">
                    <tbody>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">企业微信WebHookKey</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="webhook_config_key"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;padding-left: 48px;" colspan="2">
                                <button class="btn btn-primary" onclick="formsubmit(2)">保存</button>&nbsp;&nbsp;&nbsp;&nbsp;
                            </td>
                        </tr>
                    </tbody>
                </table>
                <table id="wifi_config" style="width: 100%;margin-top: 15px;border-collapse: collapse; display: none;">
                    <tbody>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi名称</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_name_1"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi密码</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_pswd_1"/></td>
                        </tr>
                        <!-- <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi名称(备用1)</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_name_2"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi密码(备用1)</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_pswd_2"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi名称(备用2)</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_name_3"/></td>
                        </tr>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px; padding-left: 48px; width: 20%">wifi密码(备用2)</td>
                            <td style="padding: 12px; width: 80%; float: left;"><input type="text" id="wifi_config_pswd_3"/></td>
                        </tr> -->
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 12px;padding-left: 48px;" colspan="2">
                                <button class="btn btn-primary" onclick="formsubmit(2)">保存</button>&nbsp;&nbsp;&nbsp;&nbsp;
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div style="text-align: center; margin-top: 20px; padding: 10px; background-color: #f5f6fa;">
                <a href="/t16" style="color: #3498db; text-decoration: none; font-size: 20px;">直接控制点击头</a>
            </div>
        </div>
        
    </div>
    
</body>
</html>